﻿using DevExpress.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DotNetExtensions.AspNet.DevExpress.MVVM.ViewModels
{
    public static class DocumentManagerServiceExtensions
    {
        public static IDocument ShowExistingEntityDocument<TEntity, TPrimaryKey>(this IDocumentManagerService documentManagerService, object parentViewModel, TPrimaryKey primaryKey)
        {
            IDocument document = FindEntityDocument<TEntity, TPrimaryKey>(documentManagerService, primaryKey) ?? CreateDocument<TEntity>(documentManagerService, primaryKey, parentViewModel);
            if (document != null)
                document.Show();
            return document;
        }

        public static void ShowNewEntityDocument<TEntity>(this IDocumentManagerService documentManagerService, object parentViewModel, Action<TEntity> newEntityInitializer = null)
        {
            IDocument document = CreateDocument<TEntity>(documentManagerService, newEntityInitializer ?? (x => DefaultEntityInitializer(x)), parentViewModel);
            if (document != null) document.Show();
        }

        public static void ShowNewEntityDocument(this IDocumentManagerService documentManagerService, object parentViewModel, string documentType, Action newEntityInitializer = null)
        {
            IDocument document = CreateDocument(documentManagerService, documentType, newEntityInitializer, parentViewModel);
            if (document != null) document.Show();
        }

        public static IDocument FindEntityDocument<TEntity, TPrimaryKey>(this IDocumentManagerService documentManagerService, TPrimaryKey primaryKey)
        {
            if (documentManagerService == null)
                return null;
            foreach (IDocument document in documentManagerService.Documents)
            {
                ISingleObjectViewModel<TEntity, TPrimaryKey> entityViewModel = document.Content as ISingleObjectViewModel<TEntity, TPrimaryKey>;
                if (entityViewModel != null && object.Equals(entityViewModel.PrimaryKey, primaryKey))
                    return document;
            }
            return null;
        }

        static void DefaultEntityInitializer<TEntity>(TEntity entity) { }

        static IDocument CreateDocument<TEntity>(IDocumentManagerService documentManagerService, object parameter, object parentViewModel)
        {
            if (documentManagerService == null)
                return null;
            var document = documentManagerService.CreateDocument(GetDocumentTypeName<TEntity>(), parameter, parentViewModel);
            document.Id = "_" + Guid.NewGuid().ToString().Replace('-', '_');
            document.DestroyOnClose = false;
            return document;
        }
        static IDocument CreateDocument(IDocumentManagerService documentManagerService,string documentType, object parameter, object parentViewModel)
        {
            if (documentManagerService == null)
                return null;
            var document = documentManagerService.CreateDocument(documentType, parameter, parentViewModel);
            document.Id = "_" + Guid.NewGuid().ToString().Replace('-', '_');
            document.DestroyOnClose = false;
            return document;
        }
        public static string GetDocumentTypeName<TEntity>()
        {
            return typeof(TEntity).Name.Replace("Model","") + "View";
        }
    }
}
